class Person:
    instances_created = 0					# 这个变量可以测试这个类被初始化了几次

    def __new__(cls, *args, **kwargs):
        print("创建了一个对象")
        print("__new__():", cls, args, kwargs)
        instance = super().__new__(cls)			# super负责调用上层的类，即被继承的类
        instance.number = cls.instances_created
        cls.instances_created += 1
        return instance

    def __init__(self, attribute):
        print("初始化一个对象")
        print("__init__():", self, attribute)
        self.attribute = attribute

    def __del__(self):
        print("对象被释放了")


class PP:
    def __new__(cls, *args, **kwargs):
        print("创建对象PP")
    def __init__(self):
        print("初始化对象PP")
    def __del__(self):
        print("释放对象PP")


class PPP:
    def __new__(cls, *args, **kwargs):
        print("创建对象PPP")
        return super().__new__(cls)
    def __init__(self):
        print("初始化对象PPP")
    def __del__(self):
        print("释放对象PPP")

p1 = Person("test #1")
p2 = Person("test #2")
print(p1.instances_created, p1.attribute)
print(p2.instances_created, p2.attribute)

p3 = PP()
p4 = PP()

p5 = PPP()
p6 = PPP()

# 运行结果
'''
创建了一个对象
__new__(): <class '__main__.Person'> ('test #1',) {}      
初始化一个对象
__init__(): <__main__.Person object at 0x03092F70> test #1
创建了一个对象
__new__(): <class '__main__.Person'> ('test #2',) {}
初始化一个对象
__init__(): <__main__.Person object at 0x03092FB8> test #2
2 test #1
2 test #2
创建对象PP
创建对象PP
对象被释放了
对象被释放了
'''